<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\Config;
use think\Exception;
use think\exception\PDOException;
use TrytoMqtt\Client;

class ManuallyOperatingEquipmentController extends Controller
{
    /**
     * 2021/3/17--14:35
     * @effect:监听设备
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:subscribe
     */
    public function subscribe()
    {
        $mqtt_json = Config::where(["name" => "mqtt"])->value("val");
        if ($mqtt_json) {
            $mqtt_config = json_decode($mqtt_json, 1);
        } else {
            $mqtt_config = [];
            return json_encode(["code"=>2,"msg"=>"mqtt未配置"]);
        }
        $options = [
            'clean_session' => false,
            'client_id' => 'manually-subscribe-123456',
            'username' => $mqtt_config['mqtt_user_name'],
            'password' => $mqtt_config['mqtt_password'],
        ];
        $ip = gethostbyname($mqtt_config['mqtt_host']);
        $mqtt = new Client($ip, $mqtt_config['port'], $options);
        $mqtt->onConnect = function ($mqtt) {
            $mqtt->subscribe('server/+');
            $mqtt->subscribe('realTime/+');
            $mqtt->subscribe('alarm/+');
        };
        $mqtt->onMessage = function ($topic, $content) {
            $this->receipt($topic, $content);
        };
        $mqtt->onError = function ($exception) use ($mqtt) {
            echo "error\n";
            $mqtt->reconnect(1000);
        };
        $mqtt->onClose = function () {
            echo "close\n";
        };
        $mqtt->connect();
    }

    /**
     * 回执
     * @param $topic
     * @param $content
     * @throws Exception
     * @throws PDOException
     */
    public function receipt($topic, $content)
    {
        $arr = json_decode($content, 1);

        if (!empty($arr) && isset($arr['data'])) {
            $data = $arr['data'];
            if (!empty($data)) {
                var_dump($topic);
                echo PHP_EOL;
                var_dump($content);
                echo PHP_EOL;
                switch ($topic) {
                    case 'server/9999'://拉合闸回执/设备添加回执
                        if (isset($arr['flag'])) {//绑定网关
                            switch ($arr['flag']) {
                                case 1://添加设备
                                    //100ms后执行此函数
                                    echo '-----添加设备-----';
                                    echo PHP_EOL;
                                    swoole_timer_after(100, function () use ($data) {
                                        ManuallyService::addEquipment($data);
                                    });
                                    break;
                                case 2://接受上线指令,上报表号
                                    echo '-----上报设备-----';
                                    echo PHP_EOL;
                                    swoole_timer_after(100, function () use ($data) {
                                        ManuallyService::addEquipment($data);
                                    });
                                    break;
                                case 3://校时信息
                                    echo '-----校对时间-----';
                                    echo PHP_EOL;
                                    ManuallyService::proofreadingTime($data);
                                    break;
                                    break;
                            }
                        } else {
                            echo '-----操作设备-----';
                            echo PHP_EOL;
                            ManuallyService::equipmentReceipt($data, $arr);//操作设备
                        }
                        break;
                    case 'realTime/9999'://实时数据回执
                        echo '-----采集实时数据-----';
                        echo PHP_EOL;
                        ManuallyService::realReceipt($data, $arr);
                        break;
                    case 'alarm/9999'://设备报警

                        if (isset($arr['flag'])) {
                            echo '-----采集数据异常-----';
                            echo PHP_EOL;
                            ManuallyService::addCollectError($data, $arr);
                        } else {
                            echo '-----异常数据入库-----';
                            echo PHP_EOL;
                            MqttService::insert_data2($content, $topic);
                            echo '-----设备报警-----';
                            echo PHP_EOL;
                            ManuallyService::alarmReceipt($data, $arr);
                        }

                        break;
                }

            } else {
                echo '消息体不能为空';
                echo PHP_EOL;
            }

        } else {
            var_dump($content) . '-----错误格式-----';
            echo '数据格式不正确';
            echo PHP_EOL;
        }

    }

}
